//JavaScript代码区域
layui.use(['element', 'form', 'carousel', 'layer', 'util', 'laytpl'], function() {
			var element = layui.element;
			var carousel = layui.carousel;
			var layer = layui.layer;
			var util = layui.util;
			var $ = layui.$
			var form = layui.form;
			var laytpl = layui.laytpl;
			//示例
			var endTime = new Date().getTime() + 60 * 1000 * 60 //假设为结束日期
			var serverTime = new Date().getTime(); //假设为当前服务器时间，这里采用的是本地时间，实际使用一般是取服务端的
			var totalTimer;
			var studentNo;
			var paperId;
		
			//一启动就进行判断userkey
			
			var studentInfoTpl = document.getElementById("studentInfo").innerHTML;
			var userKey = JSON.parse(sessionStorage.getItem("userKey"));
			console.log(userKey)
			if(userKey) {
				//如果存在,则说明登录过,先比对过期时间
				var delayTime = userKey.delayTime
				var nowTime = new Date().getTime()
				studentNo = userKey.studentNo;
				if(nowTime < delayTime) {
					//登录未失效
					laytpl(studentInfoTpl).render(userKey, function(html) {
						$("#studentInfoView").html(html)
						element.render()
						form.render()
					})
				}
			}
			var allPaperTpt = allPaper.innerHTML;
			var paperListTpt = paperList.innerHTML;
			$.ajax({
				type: "put",
				url: "http://localhost:8888/studentPaper/paperOfStudent/" + userKey.studentNo,
				success: function(data) {
					laytpl(allPaperTpt).render(data, function(html) {
						$("#view2").html(html)
						form.render()
					})
				}
			});
			
			$.ajax({
				type: "get",
				url: "http://localhost:8888/paper/getPaperList",
				success: function(data) {
					laytpl(paperListTpt).render(data, function(html) {
						$("#view3").html(html)
						form.render()
					})
				}
			})
			
			
			$("#submitRequest").click(function(){
				if($("#paperIdHidden").val()!=""){
					$.ajax({
						type: "post",
						url: "http://localhost:8888/studentPaper/requestForExam",
						data: {
							'studentNo': studentNo,
							'paperId': $("#paperIdHidden").val()
						},
						success: function(data) {
							if(data.code == 0) {
								layer.msg("预约成功!")
								form.render()
								element.render()
								
							} else {
								layer.msg("您已经预约此次考试了,请勿重复预约")
							}
					
						},
						error: function() {
							layer.msg("您已经预约成功了,请勿重复预约")
						}
					})
				}
			 
			})
			
			
			form.on('select(paperList)',function(data){
				$("#paperIdHidden").val(data.value)
			})
			
			
			
			form.on('select(examList)', function(data) {
				selectExam(data.value)
			})


			
			//倒数计时方法
			function countdownTime() {
				var endTime = new Date().getTime() + 1000 * 10 * 60 //假设为结束日期
					,
					serverTime = new Date().getTime(); //假设为当前服务器时间，这里采用的是本地时间，实际使用一般是取服务端的
				util.countdown(endTime, serverTime, function(date, serverTime, timer) {
						totalTimer = timer;
						var str = date[1] + '时' + date[2] + '分' + date[3] + '秒';
						$('#countdownTime').html('剩余时间' + '<font color="red">' + str + '</font>');
						if(date[3] == 0) {
							layer.open({
								content: '测试结束',
								btn: ['确认交卷'],
								yes: function(index, layero) {
									//按钮【按钮一】的回调
									$.ajax({
										type: "post",
										url: "http://localhost:8888/studentPaper/setScore",
										async: false,
										data: {
											'studentNo': studentNo,
											'paperId': paperId,
											'answer': answer
										},
										success: function(data) {
											layer.msg('提交成功')
											//						console.log(data)
											setTimeout(function() {
												window.location.href = 'index.html';
											}, 2000)
											//						
										}

									});
								},
								cancel: function() {
									//右上角关闭回调
									$.ajax({
										type: "post",
										url: "http://localhost:8888/studentPaper/setScore",
										async: false,
										data: {
											'studentNo': studentNo,
											'paperId': paperId,
											'answer': answer
										},
										success: function(data) {
											layer.msg('提交成功')
											//						console.log(data)
											setTimeout(function() {
												window.location.href = 'index.html';
											}, 2000)
											//						
										}

										//return false 开启该代码可禁止点击该按钮关闭
									})
								}
							})
							}
						});
				}

				function selectExam(id) {
					paperId = id;
					if(id) {
						console.log("ok")
					}
					var getTpl = paper.innerHTML;
					layer.confirm('您确定要开始测试吗？', {
						btn: ['是的', '取消'] //按钮
					}, function() {
						$("#submitExam").css("display", "")
						clearInterval(totalTimer)
						countdownTime();
						layer.msg('已经开始', {
							icon: 1
						});
					}, function() {

					});

					$.ajax({
						type: "get",
						url: "http://localhost:8888/paper/getPaper/" + id,
						async: false,
						success: function(data) {
							laytpl(getTpl).render(data, function(html) {
							
								$("#view").html(html)
							})
							form.render()
							//								console.log(data)
						}

					});
				}

				$("#submitExam").click(function() {
					var inputList = document.getElementsByTagName("input");
					var answer = new Array();
					for(var i = 0; i < inputList.length; i++) {
						var elm = inputList[i];
						if(elm.checked) {
							console.log(elm.value)
							answer.push(elm.value)
						}
					}
					//					console.log(answer.join(","))
					var answer = answer.join(",");
					$.ajax({
						type: "post",
						url: "http://localhost:8888/studentPaper/setScore",
						async: false,
						data: {
							'studentNo': studentNo,
							'paperId': paperId,
							'answer': answer
						},
						success: function(data) {
							layer.msg('提交成功')
							//						console.log(data)
							setTimeout(function() {
								window.location.href = 'index.html';
							}, 2000)
							//						
						}

					});

				})

			});

		function logout() {
			sessionStorage.clear();
			window.history.href = '../index.html'
		}